﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using ProductionManage.Models.Products;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Wang.Dto;
using wangyaxin.Zhangduo.MdItemType.Dto;
using wangyaxin.Zhangduo.WorkShops;
using wangyaxin.Zhangduo.WorkShops.Dto;

namespace wangyaxin.Zhangduo
{
    [ApiExplorerSettings(GroupName = "张铎")]
    public class WorkShopAppService : ApplicationService, IWorkShopAppService
    {
        public readonly IRepository<WorkShop, int> _workShopRepository;
        public WorkShopAppService(IRepository<WorkShop, int> workShopRepository)
        {
            _workShopRepository = workShopRepository;
        }

        /// <summary>
        /// 显示车间信息
        /// </summary>
        /// <param name="WorkShopCode">编号</param>
        /// <param name="WorkShopName">名称</param>
        /// <returns>返回车间</returns>
        [HttpGet, Route("GetWorkShoppService")]
        public async Task<string> GetWorkShoppService(string? WorkShopCode, string? WorkShopName)
        {
            var workShop = await _workShopRepository.GetListAsync();
            if (!string.IsNullOrEmpty(WorkShopCode))
            {
                workShop = workShop.Where(c => c.WorkShopCode.Contains(WorkShopCode)).ToList();
            }
            if (!string.IsNullOrEmpty(WorkShopName))
            {
                workShop = workShop.Where(c => c.WorkShopName.Contains(WorkShopName)).ToList();
            }
            var workShopList = ObjectMapper.Map<List<WorkShop>, List<WorkShopDto>>(workShop);
            return JsonConvert.SerializeObject(new PageDto<WorkShopDto> { code = "", data = workShopList, msg = "", count = workShopList.Count });
        }
        /// <summary>
        /// 添加车间信息
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost, Route("AddWorkShoppService")]
        public  async Task<string> AddWorkShoppService(WorkShop model)
        {
            var workshop = new WorkShop
            {
                WorkShopCode = model.WorkShopCode,
                WorkShopName = model.WorkShopName,
                Area = model.Area,
                Charger = model.Charger,
                IsEnable = model.IsEnable,
                Remark = model.Remark,
            };
            await _workShopRepository.InsertAsync(workshop);
            var workshopDto = ObjectMapper.Map<WorkShop, WorkShopDto>(workshop);
            return JsonConvert.SerializeObject(new PageDto<WorkShopDto>
            {
                code = "", // 可以根据需要设置状态码
                data = new List<WorkShopDto> { workshopDto }, // 返回添加的用户信息
                msg = "添加成功", // 可以根据需要添加消息
                count = 1 // 添加的用户数量
            });
        }
        /// <summary>
        /// 删除车间信息
        /// </summary>
        /// <param name="id">车间id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost, Route("DelWorkShopp")]
        public async Task<int> DelWorkShopp(int id)
        {
           var workShop=await _workShopRepository.GetAsync(c=>c.Id==id);
           await _workShopRepository.DeleteAsync(workShop,true);
            return 1;
        }
        /// <summary>
        /// 获取单条车间信息
        /// </summary>
        /// <param name="id">车间id</param>
        /// <returns>返回单条车间信息</returns>
        [HttpGet, Route("getWorkShopBy")]
        public async Task<WorkShop> getWorkShopBy(int id)
        {
            var workShop = await _workShopRepository.GetAsync(c => c.Id == id);
            return workShop;
        }
        /// <summary>
        /// 修改车间信息
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost, Route("UpdWorkShopBy")]
        public async Task<int> UpdWorkShopBy(UpdateWorkShopDto model)
        {
            var workshop = await _workShopRepository.GetAsync(c => c.Id == model.Id);
            workshop.WorkShopName = model.WorkShopName;
            workshop.WorkShopCode = model.WorkShopCode;
            workshop.Area=model.Area;
            workshop.Charger=model.Charger;
            workshop.Remark=model.Remark;
            workshop.IsEnable=model.IsEnable;
            await _workShopRepository.UpdateAsync(workshop);
            return 1;
        }
    }
}
